'use strict';
/** 挑战地图配置数据 */
global.CHAPTER || (global.CHAPTER = []);
/** 挑战地图断线用户缓存 */
global.CHAPTERC || (global.CHAPTERC = {});
/** 颜色游戏的消息 */
module.exports.run = function(d,s){
    const bs = require("../module/base"), cf = require("../config/color.json"), chapter = require("../config/chapter.json"), bag = require("./bag"), task = require("./task"), mail = require("./mail");
    switch(d[1]){
        case 1:{ //关卡开始
            if(!bs.checkParam(["lv"], d)) return;
            if(d[2].lv < 1 || d[2].lv > cf.maxlevel) {
                LOG.error("level error", d[2].lv);
                return;
            }
            //上次关卡缓存判断
            //if(s.U.game.index == d[2].lv){
            //    return bs.send(s, d);
            //}
            //判断最大关卡
            if(d[2].lv > s.U.game.chapter.length + 1){
                return bs.sendE(s, 203);
            }
            //体力不够
            if(s.U.bag["3"] < cf.powerchapter){ 
                return bs.sendE(s, 202);
            }
            //判断体力扣除体力
            bag.add([[3, -cf.powerchapter]], s, null, 7, d[2].lv);
            s.U.game.index = d[2].lv; //关卡
            s.U.game.indextime = Date.now(); //关卡开始时间
            bs.send(s, d);
            //游戏对局
            task.events(7, s);
            break;
        }case 2:{ //保存关卡数据
            if(!bs.checkParam(["k","v"], d)) return;
            if(s.U.game.index == 0 || d[2].k != s.U.game.index){
                LOG.error("level error begin <> end", s.U.game.index);
                return;
            }
            let tm = Date.now() - s.U.game.indextime;
            if(s.U.game.chapter[d[2].k]){
                if(/*s.U.game.chapter[d[2].k][0] > tm || */s.U.game.chapter[d[2].k][1] > d[2].v) s.U.game.chapter[d[2].k] = [tm, d[2].v];
            }else{
                s.U.game.chapter[d[2].k] = [tm, d[2].v];
                //通过关卡
                task.events(5, s);
            }
            //获取过关奖励
            if(chapter[d[2].k].rw.length > 0){
                bag.add(chapter[d[2].k].rw, s, null, 12, d[2].k);
            }
            //看广告关卡
            s.U.game.lookindex = s.U.game.index;
            //三星过关
            if(d[2].v <= chapter[d[2].k].star[0]){
                task.events(2, s);
            }
            s.U.game.index = 0;
            d[2] = 0;
            bs.send(s, d);
            break;
        }case 3:{ //设置携带的图鉴
            if(s.U.bag[d[2]] > 0){
                const bagc = require("../config/itemcolor.json");
                if("color" in bagc[d[2]]){
                    s.U.game.color[bagc[d[2]].color] = d[2];
                }
                d[2] = s.U.game.color;
                bs.send(s, d);
            }
            break;
        }case 4:{ //加载最新的关卡列表
            loadChapter(()=>{
                let p = d[2];
                if(p < 1 || !p) p = 1;
                p *= 10;
                d[2] = [CHAPTER.length];
                for(let i = p - 10;i < p && i < CHAPTER.length;i++){
                    d[2].push({id:CHAPTER[i].chapterid,title:CHAPTER[i].title,pay:CHAPTER[i].pay,run:CHAPTER[i].run,anum:CHAPTER[i].anum,star:CHAPTER[i].plval/(CHAPTER[i].plnum||1)|0});
                }
                bs.send(s, d);
            })
            break;
        }case 5:{ //编辑完保存关卡数据
            if(!bs.checkParam(["t", "lv", "sval", "pay", "anum", "title", "jval"], d)) return;
            if(d[2].t == 2){ //公有关卡加入到缓存
                let pall = d[2].pay * d[2].anum;
                if(pall <= 0){
                    return bs.sendE(s, 15);
                }
                if(pall <= s.U.bag[2]){
                    bs.DB.query("INSERT INTO cchapter(userid,run,rsuccess,rfinish,anum,rval,runall,pay,win,plnum,plval,ctp,title,createtime,chapterval,lv) VALUES($1,0,0,0,$2,$3,0,$4,'{}',0,0,2,$5,now(),$6,$7) RETURNING *",(rs)=>{
                        if(rs.rows.length > 0){
                            bag.add([[2,-pall]], s, null, 16, rs.rows[0].chapterid);
                            CHAPTER.unshift(rs.rows[0]);
                            d[2] = rs.rows[0].chapterid;
                            bs.send(s, d);
                        }
                    },[s.U.uid, d[2].anum, d[2].sval, d[2].pay, d[2].title, d[2].jval, d[2].lv]);
                }else{
                    return bs.sendE(s, 4);
                }
            }else{
                bs.DB.query("INSERT INTO cchapter(userid,run,rsuccess,rfinish,anum,rval,runall,pay,win,plnum,plval,ctp,title,createtime,chapterval,lv) VALUES($1,0,0,0,0,$2,0,0,'{}',0,0,1,$3,now(),$4,$5) RETURNING chapterid",(rs)=>{
                    d[2] = rs.rows[0].chapterid;
                    bs.send(s, d);
                },[s.U.uid, d[2].sval, d[2].title, d[2].jval, d[2].lv]);
            }
            break;
        }case 6:{ //加载指定关卡数据
            if(s.U.game.run && s.U.game.run.id != d[2]) return bs.sendE(s, 208); //判断是否有正在玩的关卡

            if(d[2] && d[2] > 0){//请求指定的关卡
                for(let i=0;i<CHAPTER.length;i++){ //查找缓存中关卡
                    if(CHAPTER[i].chapterid == d[2]){
                        //关卡已经胜利过了
                        if(CHAPTER[i].win && CHAPTER[i].win.indexOf(s.U.uid) != -1){ 
                            return bs.sendE(s, 204);
                        }
                        //是否是加载关卡数据
                        if(s.U.game.run){
                            d[2] = [CHAPTER[i].rval,CHAPTER[i].chapterval];
                            bs.send(s, d);
                            return;
                        }
                        //检查道具数量
                        if(s.U.bag[2] && s.U.bag[2] >= CHAPTER[i].pay){
                            //非自己闯关扣钻石
                            if(s.U.uid != CHAPTER[i].userid){
                                bag.add([[2,-CHAPTER[i].pay]], s, null, 15, CHAPTER[i].chapterid);
                                CHAPTER[i].run++;
                                CHAPTER[i].runall++;
                            } 
                            d[2] = [CHAPTER[i].rval,CHAPTER[i].chapterval];
                            s.U.game.run = {id:CHAPTER[i].chapterid,userid:CHAPTER[i].userid,t:Date.now(),pay:CHAPTER[i].pay,n:CHAPTER[i].title,v:CHAPTER[i].rval};
                            bs.send(s, d);
                            //检查挑战次数是否用完
                            if(CHAPTER[i].run >= CHAPTER[i].anum){
                                saveChapter(i);
                                CHAPTER.splice(i, 1);
                            }else if(i > 0){
                                let tmp = CHAPTER[i];
                                CHAPTER[i] = CHAPTER[i - 1];
                                CHAPTER[i - 1] = tmp; 
                            }
                            return;
                        }else{
                            return bs.sendE(s, 4);
                        }
                    }
                }
                //在线缓存中搜索不到关卡 检查数据库
                bs.DB.query("SELECT * from cchapter WHERE chapterid = $1", (rs)=>{
                    if(rs.rows.length < 1){
                        return bs.sendE(s, 205);
                    }
                    //是否是加载关卡数据
                    if(s.U.game.run){
                        d[2] = [rs.rows[0].rval,rs.rows[0].chapterval];
                        bs.send(s, d);
                        return;
                    }
                    //公有关卡没有缓存的只能自己查看
                    if(rs.rows[0].ctp == 2){
                        if(rs.rows[0].userid != s.U.uid){
                            if(rs.rows[0].run >= rs.rows[0].anum){
                                return bs.sendE(s, 206);
                            }else{
                                return bs.sendE(s, 207);
                            }
                        }
                    }

                    s.U.game.run = {id:rs.rows[0].chapterid,userid:rs.rows[0].userid,t:Date.now(),pay:rs.rows[0].pay,n:rs.rows[0].title,v:rs.rows[0].rval};
                    d[2] = [rs.rows[0].rval,rs.rows[0].chapterval];
                    bs.send(s, d);
                }, [d[2]]);
            }else{
                return bs.sendE(s, 15);
            }
            break;
        }case 7:{ //通过指定关卡 rval:-1 放弃挑战
            if(!bs.checkParam(["rval", "star", "con"], d)) return bs.sendE(s, 15);
            if(!s.U.game.run) return;
            if(d[2].rval < -1 || d[2].con.length > 128) return bs.sendE(s, 15);
            if(d[2].star > 5) d[2].star = 5;
            //自己挑战什么都不处理
            if(s.U.game.run.userid == s.U.uid){
                delete s.U.game.run;
                return;
            } 

            //保存挑战记录
            let success = 0;
            if(d[2].rval == -1) success = 2;
            else if(d[2].rval < s.U.game.run.v){ //挑战成功
                success = 1;
            } 
            //更新关卡数据
            let i=0;
            for(;i<CHAPTER.length;i++){ //查找缓存中关卡
                if(CHAPTER[i].chapterid == s.U.game.run.id){
                    CHAPTER[i].rfinish++;
                    if(success == 1){
                        CHAPTER[i].win.push(s.U.uid);
                        CHAPTER[i].rsuccess++;
                    } 
                    if(d[2].star > 0){
                        CHAPTER[i].plnum++;
                        CHAPTER[i].plval += d[2].star;
                    }
                    break;
                }
            }
            if(i == CHAPTER.length){
                bs.DB.query("UPDATE cchapter SET rsuccess=rsuccess+$5,rfinish=rfinish+1,win=CASE WHEN $5=1 AND $1 != ALL(win) THEN ARRAY_APPEND(win, $1) ELSE win END,plnum=plnum+$3,plval=plval+$4 WHERE chapterid=$2 RETURNING rsuccess,anum,rfinish,userid,ctp,title,pay", (rs)=>{
                    if(rs.rows.length > 0){
                        //大厅关卡被挑战完成
                        if(rs.rows[0].ctp == 2 && rs.rows[0].rfinish == rs.rows[0].anum){
                            let item = [], gnum = rs.rows[0].anum-rs.rows[0].rsuccess;
                            if(gnum > 0) item[0] = [2, rs.rows[0].pay*gnum];
                            mail.send(1, 0, rs.rows[0].userid, '挑战结束', '您创建关卡 ['+rs.rows[0].title+'] 已被人挑战完成 成功:'+rs.rows[0].rsuccess+' 失败:'+gnum+' 获得钻石奖励 ' + (item[0]?item[0][1]:0), JSON.stringify(item));
                        }else if(rs.rows[0].ctp == 1){

                        }
                    }
                }, [s.U.uid, s.U.game.run.id, d[2].star?1:0, d[2].star>0?d[2].star:0,success==1?1:0]);
            }
            if(s.U.game.run.pay){ //大厅关卡才有评论
                bs.DB.query("INSERT INTO cchapterpl(chapterid,userid,good,success,rtime,rval,runtime,plval,plstr) VALUES($1,$2,'{}',$3,$4,$5,now(),$6,$7)", null, 
                [s.U.game.run.id, s.U.uid, success, Date.now()-s.U.game.run.t, d[2].rval, d[2].star, d[2].con]);
            }
            //挑战成功获得奖励
            if(d[2].rval != -1 && s.U.game.run.pay){
                mail.send(1, 0, s.U.uid, '挑战成功', '成功挑战了关卡 ['+s.U.game.run.n+'] 获得钻石奖励 ' + s.U.game.run.pay*2, JSON.stringify([[2,s.U.game.run.pay*2]]));
            }
            delete s.U.game.run;
            break;
        }case 8:{ //请求游戏数据
            bs.send(s, [2,8,s.U.game]);
            break;
        }case 9:{ //刷新体力
            powerRef(s);
            break;
        }case 10:{ //获取关卡需要消耗的钻石(暂时不需要了 直接在列表中返回了)
            if(!d[2]) return;
            for(let i=0;i<CHAPTER.length;i++){ //查找缓存中关卡
                if(CHAPTER[i].chapterid == d[2]){
                    d[2] = CHAPTER[i].pay;
                    bs.send(s, d);
                    return;
                }
            }
            d[2] = 0;
            bs.send(s, d);
            break;
        }case 11:{ //加载关卡挑战记录
            if(!d[2]) return;
            bs.DB.query("SELECT plid, COALESCE(ARRAY_LENGTH(good,1),0) goods, success, rval, runtime,B.nickname,B.headimg, plval, plstr FROM cchapterpl A INNER JOIN users B ON A.userid=B.userid WHERE chapterid = $1", (rs)=>{
                bs.send(s, [d[0], d[1], rs.rows]);
            }, [d[2]]);
            break;
        }case 12:{ //评论点赞
            if(!d[2]) return;
            bs.DB.query("UPDATE cchapterpl SET good = ARRAY_APPEND(good, $2) WHERE plid = $1 AND $2 != ALL(good) RETURNING plid", (rs)=>{
                if(rs.rows.length > 0){
                    bs.send(s, d);
                }
            }, [d[2], s.U.uid]);
            break;
        }case 13:{ //加载自己创建的关卡
            bs.DB.query("SELECT chapterid,title,ctp,lv,createtime,anum,run,plnum,plval,rsuccess,rfinish,pay FROM cchapter WHERE userid = $1 ORDER BY chapterid DESC LIMIT 50", (rs)=>{
                bs.send(s, [d[0], d[1], rs.rows]);
            }, [s.U.uid]);
            break;
        }case 14:{ //广告看完
            if(s.U.game.lookindex){
                //获取过关奖励
                if(chapter[s.U.game.lookindex].rw.length > 0){
                    bag.add(chapter[s.U.game.lookindex].rw, s, null, 17, s.U.game.lookindex);
                }
                delete s.U.game.lookindex;
            }
            break;
        }
    }
}
/** 登录事件 */
module.exports.login = function(s){
    const cf = require("../config/color.json"),bs = require("../module/base"), bag = require("./bag");
    //获取用户属性
    if(!s.U.game){
        s.U.game = {"powerlast":0,"lv":1,"boxnum":1,"index":0,"color":{},"chapter":[]};
        bag.add(cf.regbag, s, null, 8);
    } 
    powerRef(s);
    bag.run([3,1], s);
    if(CHAPTERC[s.U.uid]) delete CHAPTERC[s.U.uid];
    bs.send(s, [2,8,s.U.game]);
}

/** 游戏退出 */
module.exports.loginout = function(s){
    if(s.U.game.run){
        CHAPTERC[s.U.uid] = {id:s.U.game.run.id,t:s.U.game.run.t};
    }
}

/** 获得道具 */
module.exports.getitem = function(s, it){
    const itc = require("../config/itemcolor.json"), bs = require("../module/base");
    let sd = false;
    for(let item of it){
        if(item[0] > 199 && item[0] < 300){ //获得图鉴需要处理
            if(!(itc[item[0]].color in s.U.game.color)){
                s.U.game.color[itc[item[0]].color] = item[0];
                sd = true;
            }
        }
    }
    if(sd){
        bs.send(s, [2,3,s.U.game.color]);
    }
}

/** 游戏服务器关闭 */
module.exports.exit = function(){
    //更新关卡数据
    for(let i=0;i<CHAPTER.length;i++){ //查找缓存中关卡
        saveChapter(i);
    }
}

/** 加载关卡 */
function loadChapter(rfun){
    const bs = require("../module/base");
    if(CHAPTER.length > 0) rfun();
    else{
        bs.DB.query("SELECT * from cchapter WHERE run < anum AND ctp = 2 ORDER BY createtime DESC LIMIT 100", (rs)=>{
            CHAPTER = rs.rows;
            rfun();
        });
    }
}

/** 保存缓存的关卡 */
function saveChapter(i){
    const bs = require("../module/base");
    bs.DB.query("UPDATE cchapter SET run=$1,runall=$2,win=$3,plnum=$4,plval=$5,rsuccess=$7,rfinish=$8 WHERE chapterid=$6", null, 
            [CHAPTER[i].run, CHAPTER[i].runall, CHAPTER[i].win, CHAPTER[i].plnum, CHAPTER[i].plval, CHAPTER[i].chapterid, CHAPTER[i].rsuccess, CHAPTER[i].rfinish]);
}

/** 刷新体力 */
function powerRef(s){
    const cf = require("../config/color.json"), bag = require("./bag");
    let num = (Date.now() - s.U.game.powerlast) / cf.powertime | 0;
    if(num > 0){
        bag.add([[3,num]], s, null, 2);
        s.U.game.powerlast = Date.now();
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/** 生成关卡内容 */
module.exports.randChapter = function(S, N){
    LOG.info("randChapter");
    /** 右下X+ 左下Z+ */
    let CF = {
        /** 空心柱子 */
        "0":{"K":0,"spriteRes":"","materialRes":"Blue_Palette01.png","flowerSpriteRes":[],"color":"4","x":-1,"y":0.25,"z":0,"click":0,"bf":1,"bfcolor":0},
        /** 空心方块 */
        "1":{"K":1,"spriteRes":"","materialRes":"Marine_Gradient01.png","flowerSpriteRes":[],"color":64,"x":-3,"y":0,"z":1,"click":1,"bf":1,"bfcolor":0},
        /** 灰色方块 */
        "2":{"K":3,"spriteRes":"","materialRes":"","flowerSpriteRes":[],"color":0,"x":-1,"y":0,"z":1,"click":1,"bf":0,"bfcolor":0},
        /** 实心方块 */
        "3":{"K":3,"spriteRes":"","materialRes":"Green_Gradient01.png","flowerSpriteRes":[],"color":8,"x":-3,"y":0,"z":0,"click":1,"bf":0,"bfcolor":0},
        /** 实心柱子 */
        "6":{"K":6,"spriteRes":"","materialRes":"Red_Palette01.png","flowerSpriteRes":[],"color":1,"x":-2,"y":0.25,"z":1,"click":0,"bf":0,"bfcolor":0},
        /** 变色板 四种颜色中变一种 */
        "7":{"K":7,"spriteRes":"","materialRes":"Blue_Gradient01.png","flowerSpriteRes":[],"color":4,"x":0,"y":0.25,"z":1,"click":1,"bf":0,"bfcolor":0},
        /** 射线板 发射颜色 0:右下 1右上 2左上 3左下 */
        "8":{"K":8,"x":-1,"y":0.25,"z":-1,"spriteRes":"","materialRes":"Red_Gradient01.png","flowerSpriteRes":[],"color":1,"click":1,"bf":0,"bfcolor":0,"d":[0,1]},
        /** 旋转方块板子 发射颜色 0:右下 1右上 2左上 3左下 设置多色 */
        "9":{"K":9,"x":-1,"y":0.25,"z":-1,"spriteRes":"","materialRes":"","flowerSpriteRes":[],"color":0,"click":1,"bf":0,"bfcolor":0,"d":[1,2,4,8]},
        /** 彩虹柱子 任意颜色 */
        "10":{"K":10,"x":0,"y":0,"z":0,"spriteRes":"","materialRes":"","flowerSpriteRes":[],"color":0,"click":0,"bf":1,"bfcolor":0},
        /** 彩虹方块 任意颜色 */
        "11":{"K":11,"x":1,"y":0,"z":0,"spriteRes":"","materialRes":"","flowerSpriteRes":[],"color":0,"click":1,"bf":1,"bfcolor":0},
        /** 圆形底座镜子 0竖着 1横着的 能换方向 不能改位置 */
        "12":{"K":12,"x":-1,"y":0,"z":-1,"spriteRes":"","materialRes":"","flowerSpriteRes":[],"color":0,"click":1,"bf":0,"bfcolor":0,"rd":0},
        /** 方块底座镜子 0竖着 1横着的 不能换方向 能改位置 */
        "13":{"K":13,"x":-1,"y":0,"z":-1,"spriteRes":"","materialRes":"","flowerSpriteRes":[],"color":0,"click":1,"bf":0,"bfcolor":0,"rd":0},
        /** 柱子底座镜子 0竖着 1横着的 不能换方向 不能改位置 */
        "14":{},
        /** 拉伸板 0竖着 1横着的 带颜色 挡射线 拉三次 */
        "15":{"K":15,"x":1,"y":0.25,"z":0,"spriteRes":"","materialRes":"Red_Gradient01.png","flowerSpriteRes":[],"color":1,"click":1,"bf":0,"bfcolor":0,"rd":1}
    }, CL = [
        {"v":1,"n":"Red","l":[]},
        {"v":2,"n":"Yellow","l":[]},
        {"v":4,"n":"Blue","l":[]},
        {"v":8,"n":"Green","l":[[2,4]]},
        {"v":16,"n":"Purple","l":[[1,4]]}, //紫
        {"v":32,"n":"Magenta","l":[[16,1]]}, //紫红色
        {"v":64,"n":"Marine","l":[[16,4]]}, //海洋蓝
        {"v":128,"n":"Orange","l":[[1,2]]},
        {"v":256,"n":"BlueGreen","l":[[8,4]]},
        {"v":512,"n":"YellowGreen","l":[[8,2]]}
    ], CLH = {
        "1" : 1,
        "2" : 2,
        "3" : 128,
        "4" : 4,
        "5" : 16,
        "6" : 8,
        "8" : 8,
        "10" : 512,
        "12" : 256,
        "16" : 16,
        "17" : 32,
        "20" : 64,
        "32" : 32,
        "64" : 64,
        "128" : 128,
        "256" : 256,
        "512" : 512
    }, SH = [0,1,10,11], SK = [3,6,7,8,9,15], PC = [[0,1],[0,-1],[1,0],[-1,0]], MI = [1,3,11,13];
    let json = {"map":{"width":5,"height":5},"list":[]}, larr = [], maxnum = 0;
    switch(S){
        case '5':{ //-2,2
            json.map.width = json.map.height = 5;
            maxnum = 5;
            break;
        }case '8':{ //-3,3
            json.map.width = json.map.height = 8;
            maxnum = 7;
            break;
        }case '10':{
            break;
        }case '16':{ //-6,6
            json.map.width = json.map.height = 16;
            maxnum = 13;
            break;
        }
    };
    //生成生花元素
    for(let i=0;i<N;i++){
        let ii = SH.length*Math.random()|0;
        //降低彩虹生成概率
        if(ii > 1 && Math.random()*100 > 30) ii -= 2;
        let item = JSON.parse(JSON.stringify(CF[SH[ii]])), x = 0, z = 0;
        //随机位置 
        let rN = 0, color = 0;
        while(rN < 200){
            rN++;
            x = Math.random()*maxnum|0;
            z = Math.random()*maxnum|0;
            //最大最小位置不生花
            if(x == z && (x == 0 || x == maxnum - 1)) continue;
            //当前位置有元素
            if(larr[x] && larr[x][z]) continue;
            //计算当前位置颜色
            color = 0;
            if(larr[x-1] && larr[x-1][z] && SK.indexOf(larr[x-1][z].K) != -1) color += larr[x-1][z].color;
            if(larr[x+1] && larr[x+1][z] && SK.indexOf(larr[x+1][z].K) != -1) color += larr[x+1][z].color;
            if(larr[x] && larr[x][z-1] && SK.indexOf(larr[x][z-1].K) != -1) color += larr[x][z-1].color;
            if(larr[x] && larr[x][z+1] && SK.indexOf(larr[x][z+1].K) != -1) color += larr[x][z+1].color;
            //当前位置是非有效色
            if(color > 0 && !(color in CLH)) continue;
            break;
        }
        //随机生花对象不能超过200次
        if(rN >= 200) continue;

        if(!larr[x]) larr[x] = [];
        larr[x][z] = item;
        let carr = [];
        if(item.K < 10){ //指定颜色生花
            //没有颜色的时候
            if(color == 0){
                let clc = CL[CL.length*Math.random()|0];
                //有组合颜色用组合颜色
                carr = [clc.v]
                if(clc.l.length > 0 && Math.random()*100 < 50){
                    carr = clc.l[0];
                }
                larr[x][z].color = clc.v;
            }else{
                //已有单色
                if(CLH[color] == color){ 
                    //找合成的色
                    for(let m=0;m<CL.length;m++){
                        for(let hc=0;hc<CL[m].l.length;hc++){
                            if(color == CL[m].l[hc][0]){
                                if(carr.length == 0) carr.push(CL[m].l[hc][1]);
                                else if(Math.random()*100 < 50) carr[0] = CL[m].l[hc][1];
                            }
                            if(color == CL[m].l[hc][1]){
                                if(carr.length == 0) carr.push(CL[m].l[hc][0]);
                                else if(Math.random()*100 < 50) carr[0] = CL[m].l[hc][0];
                            }
                        }
                        //找到合成色
                        if(carr.length > 0){
                            larr[x][z].color = CL[m].v;
                            break;
                        } 
                    }
                    //已有单色不能合成颜色
                    if(carr.length == 0){
                        larr[x][z].color = color;
                    }
                }else{ //已有混色 这只能三色混的时候才有效
                    larr[x][z].color = CLH[color];
                    // let zcolor = clh[color];
                    // for(let m=0;m<cl.length;m++){
                    //     for(let hc=0;hc<cl[m].l.length;hc++){
                    //         if(zcolor == cl[m].l[hc][0]){
                    //             carr.push(cl[m].l[hc][1]);
                    //             break;
                    //         }
                    //         if(zcolor == cl[m].l[hc][1]){
                    //             carr.push(cl[m].l[hc][0]);
                    //             break;
                    //         }
                    //     }
                    //     if(carr.length > 0) break;
                    // }
                }
            } 
            //设置生花模型颜色
            let clc = null;
            for(let a=0;a<CL.length;a++) {
                if(larr[x][z].color == CL[a].v){
                    clc = CL[a];
                    break;
                }
            }
            if(larr[x][z].K == 0) larr[x][z].materialRes = clc.n + "_Palette01.png";
            else if(larr[x][z].K == 1) larr[x][z].materialRes = clc.n + "_Gradient01.png";
        }else{ //彩虹生花直接添加随机颜色色块
            carr.push(CL[CL.length*Math.random()|0].v);
        }   
        LOG.info("item", larr[x][z].color, larr[x][z].K, "[", x, z, "]", "carr", color, carr);         
        //生成旁边有色块             
        for(let c=0;c<carr.length;c++){
            let rx = PC.length*Math.random()|0, n = 0;
            for(;n<4;n++){
                rx++;
                rx = rx % PC.length;
                LOG.info("rx", rx);
                //检测添加颜色是否会影响其它生花
                let gf = false;
                for(let p=0;p<PC.length;p++){
                    if(PC[rx][0] + PC[p][0] == 0 && PC[rx][1] + PC[p][1] == 0) continue;
                    if(larr[x + PC[rx][0] + PC[p][0]] && larr[x + PC[rx][0] + PC[p][0]][z + PC[rx][1] + PC[p][1]] && SH.indexOf(larr[x + PC[rx][0] + PC[p][0]][z + PC[rx][1] + PC[p][1]].K) != -1){
                        gf = true;
                        LOG.info("p", p);
                        break;
                    }
                }
                if(gf) continue;
                //检查对象是否在地图范围内
                if(x + PC[rx][0] < 0 || z + PC[rx][1] < 0 || x + PC[rx][0] >= maxnum || z + PC[rx][1] >= maxnum) continue;
                //当前位置没有元素添加元素
                if(!(larr[x + PC[rx][0]] && larr[x + PC[rx][0]][z + PC[rx][1]])){
                    if(!larr[x + PC[rx][0]]) larr[x + PC[rx][0]] = [];
                    let cl = 2;
                    if(carr[c] < 16) cl = 3;
                    larr[x + PC[rx][0]][z + PC[rx][1]] = JSON.parse(JSON.stringify(CF[SK[cl*Math.random()|0]]));
                    larr[x + PC[rx][0]][z + PC[rx][1]].color = carr[c];
                    //找颜色对象
                    let clc = null;
                    for(let a=0;a<CL.length;a++) {
                        if(carr[c] == CL[a].v){
                            clc = CL[a];
                            break;
                        }
                    }
                    //修改模型颜色
                    if(larr[x + PC[rx][0]][z + PC[rx][1]].K == 3) larr[x + PC[rx][0]][z + PC[rx][1]].materialRes = clc.n + "_Gradient01.png";
                    else if(larr[x + PC[rx][0]][z + PC[rx][1]].K == 6) larr[x + PC[rx][0]][z + PC[rx][1]].materialRes = clc.n + "_Palette01.png";
                    else if(larr[x + PC[rx][0]][z + PC[rx][1]].K == 7) larr[x + PC[rx][0]][z + PC[rx][1]].materialRes = clc.n + "_Gradient01.png";
                    LOG.info("color item", carr[c], larr[x + PC[rx][0]][z + PC[rx][1]].K, "[", x + PC[rx][0], z + PC[rx][1], "]");
                    break;
                }
            }
            //色块摆放失败
            if(n == 4) larr[x][z] = null;
        } 
    }
    //修改色块为射线

    //随机添加灰色块
    for(let i=0;i<maxnum;i++){
        for(let j=0;j<maxnum;j++){
            if(!(larr[i] && larr[i][j]) && Math.random()*100 < 50){
                if(!larr[i]) larr[i] = [];
                larr[i][j] = JSON.parse(JSON.stringify(CF[2]));
            }
            if(larr[i] && larr[i][j]){
                if(maxnum == 5){
                    larr[i][j].x = i - 2;
                    larr[i][j].z = j - 2;
                }else if(maxnum == 7){
                    larr[i][j].x = i - 3;
                    larr[i][j].z = j - 3;
                }else if(maxnum == 13){
                    larr[i][j].x = i - 6;
                    larr[i][j].z = j - 6;
                }
                json.list.push(larr[i][j]);
            }
        }
    }
    LOG.info(JSON.stringify(json));
    //随机更换位置
    for(let r = 0;r < json.list.length * 3;r++){
        let p1 = Math.random()*json.list.length | 0, p2 = Math.random()*json.list.length | 0;
        if(p1 != p2 && MI.indexOf(json.list[p1].K) != -1 && MI.indexOf(json.list[p2].K) != -1){
            let t = json.list[p2].x;
            json.list[p2].x = json.list[p1].x;
            json.list[p1].x = t;
            t = json.list[p2].z;
            json.list[p2].z = json.list[p1].z;
            json.list[p1].z = t;
        }
    }
    LOG.info(JSON.stringify(json));
    return JSON.stringify(json);
}
